package rankingGenerator;

import java.util.ArrayList;


import querydatabaseselection.pair.QueryDatabasePair;
import rankingGenerator.statistics.DREComparableStatistic;
import distributedRE.model.DataBase;
import distributedRE.model.DistributedREModel;
import distributedRE.model.Query;
import distributedRE.model.Session;

public abstract class RankingGenerator {

	
	private ArrayList<Query> arrq;
	private ArrayList<DataBase> arrdb;
	private ArrayList<DREComparableStatistic> arrval;
	private ArrayList<Integer> order;
	
	
	public abstract DREComparableStatistic calculateCostValue(Query query, DataBase dataBase,
			DistributedREModel dREModel,Session s);

	public void generateRanking(){
		
		ArrayList<Integer> noSelectedPositions = new ArrayList<Integer>();
		
		for (int i = 0; i < arrval.size(); i++) {
			
			noSelectedPositions.add(new Integer(i));
		
		}
		
		for (int i = 0; i < arrval.size(); i++) {
			
			DREComparableStatistic bestval = arrval.get(noSelectedPositions.get(0).intValue());
			
			int bestpos = noSelectedPositions.get(0).intValue();
			
			for (Integer position : noSelectedPositions) {
				
				int pos = position.intValue();
				
				if (arrval.get(pos).compareTo(bestval)>0){
					
					bestval = arrval.get(pos);
					
					bestpos = pos;
				
				}
				
			}
		
			noSelectedPositions.remove(new Integer(bestpos));
			
			order.add(new Integer(bestpos));
			
		}
		
	}

	public void startNewSession() {
		
		if (arrq == null)
			arrq = new ArrayList<Query>();
		if (arrdb == null)
			arrdb = new ArrayList<DataBase>();
		if (arrval == null)
			arrval = new ArrayList<DREComparableStatistic>();
		if (order == null)
			order = new ArrayList<Integer>();
		
		arrq.clear();
		arrdb.clear();
		arrval.clear();
		
	}

	public ArrayList<QueryDatabasePair> calculatedValues() {
		
		ArrayList<QueryDatabasePair> ret = new ArrayList<QueryDatabasePair>();
		
		for (Integer pos : order) {
			ret.add(new QueryDatabasePair(arrq.get(pos.intValue()), arrdb.get(pos.intValue()),arrval.get(pos.intValue())));
		}
		
		return ret;
		
	}

	public void storeCostValue(Query query, DataBase dataBase,
			DREComparableStatistic value) {
		
		arrq.add(query);
		arrdb.add(dataBase);
		arrval.add(value);
		
	}

}
